package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.ForbiddenException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.dto.InvoiceDTO;
import io.github.talelin.latticy.entity.Invoice;
import io.github.talelin.latticy.mapper.InvoiceMapper;
import io.github.talelin.latticy.model.InvoiceDO;
import io.github.talelin.latticy.repository.InvoiceRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;

@Service
public class InvoiceService extends ServiceImpl<InvoiceMapper, InvoiceDO> {

    @Autowired
    private InvoiceRepository invoiceRepository;

    @Autowired
    private RecordService recordService;

    @Autowired
    private UserUtil userUtil;


    @Transactional
    public void create(InvoiceDTO dto) {
        Invoice invoice = new Invoice();
        Date now = new Date();
        Long userId = UserUtil.getCurrentUserId();
        Long rootUserId = userUtil.getRootUserId();
        BeanUtils.copyProperties(dto, invoice);
        invoice.setUserId(userId);
        invoice.setExamineUserId(rootUserId);
        invoice.setStatus(1);
        invoice.setCreateTime(now);
        invoiceRepository.save(invoice);
        //同步记录
        recordService.create(userId, rootUserId, rootUserId, 3, invoice.getId());
    }

    public void updateStatus(Long id, int status) {
        Invoice invoice = this.get(id);
        invoice.setStatus(status);
        invoiceRepository.save(invoice);
    }

    public Invoice get(Long id) {
        Optional<Invoice> invoice = invoiceRepository.findById(id);
        return invoice.orElseThrow(() -> new NotFoundException());
    }

    @Transactional
    public void updated(Long id, InvoiceDTO dto) {
        Invoice invoice = this.get(id);
        if (invoice.getStatus() == 3) {
            throw new ForbiddenException(20006);
        }
        BeanUtils.copyProperties(dto, invoice);
        invoice.setStatus(1);
        invoiceRepository.save(invoice);
        //同步记录
        recordService.updateStatusToWait(3, invoice.getId());
    }

    /****************************** 后台 ******************************/
    public void add(InvoiceDTO dto) {
        InvoiceDO invoiceDO = new InvoiceDO();
        Long userId = UserUtil.getCurrentUserId();
        Long rootUserId = userUtil.getRootUserId();
        invoiceDO.setUserId(userId);
        invoiceDO.setExamineUserId(rootUserId);
        invoiceDO.setStatus(1);
        BeanUtils.copyProperties(dto, invoiceDO);
        this.save(invoiceDO);
    }

    public void edit(Long id, InvoiceDTO dto) {
        InvoiceDO invoiceDO = this.getInvoice(id);
        BeanUtils.copyProperties(dto, invoiceDO);
        this.updateById(invoiceDO);
    }

    public InvoiceDO getInvoice(Long id) {
        InvoiceDO invoiceDO = this.getById(id);
        if (invoiceDO == null) {
            throw new NotFoundException();
        }
        return invoiceDO;
    }

    public void delete(Long id) {
        this.getInvoice(id);
        this.getBaseMapper().deleteById(id);
    }

}
